import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/product_productmouldquota_odoo_api.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/models/odoo/product_productmouldquota.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/odoo/product_productunitprice/product_list.dart';
import '../../../generated/l10n.dart';

class ProductProductmouldquotaEdit extends StatefulWidget {
  ProductProductmouldquotaEdit({this.productProductmouldquota});

  final ProductProductmouldquota? productProductmouldquota;

  @override
  _ProductProductmouldquotaEditState createState() =>
      _ProductProductmouldquotaEditState();
}

class _ProductProductmouldquotaEditState
    extends State<ProductProductmouldquotaEdit> {
  late ProductProductmouldquota productProductmouldquota;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();

  //late List<SelectOptionVO> activeList;

  //车间名称
  List<SelectOptionVO> workshopIdList = [];
  //工厂名称
  List<SelectOptionVO> factoryIdList = [];
  //设备工位
  List<SelectOptionVO> stationIdList = [];

  @override
  void initState() {
    super.initState();
    this.isAdd = widget.productProductmouldquota == null;
    this.productProductmouldquota =
        widget.productProductmouldquota ?? ProductProductmouldquota();
    _loadSelectVO();
  }

//产品点击事件触发的方法
  selectProduct(isooa) async {
    showDialog(
      context: context,
      builder: (BuildContext context) => Dialog(
        child: ProductList(),
      ),
    ).then((v) {
      if (v != null) {
        //_query();
        formKey.currentState!.save();
        if (isooa!) {
          productProductmouldquota.beforeproductId = v['id'].toString();
          productProductmouldquota.beforeproductName = v['name'];
        } else {
          productProductmouldquota.afterproductId = v['id'].toString();
          productProductmouldquota.afterproductName = v['name'];
        }

        setState(() {});
      }
    });
  }

  _loadSelectVO() async {
    factoryIdList = await DynamicDataApi.getModelSelect(
        model: "hr.department",
        name: "complete_name",
        key: 'parent_id',
        val: null);

    if (productProductmouldquota.workshopId != null) {
      workshopIdList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "hr.department",
          name: "complete_name",
          key: 'parent_id',
          val: int.parse(this.productProductmouldquota.factoryId!));
    }
    if (productProductmouldquota.stationId != null) {
      stationIdList = await DynamicDataApi.getModelSelect(
          active: false,
          model: "base.productionstation",
          name: "stationname",
          key: 'workshop_id',
          val: int.parse(this.productProductmouldquota.workshopId!));
    }

    //activeList=[SelectOptionVO(value:'0',label: "禁用"),SelectOptionVO(value:'1',label: "启用")];
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            label: '工厂名称',
            dataList: factoryIdList,
            value: productProductmouldquota.factoryId,
            onSaved: (v) => {productProductmouldquota.factoryId = v},
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
            onChange: (v) async {
              if (productProductmouldquota.factoryId != v) {
                formKey.currentState!.save();
                productProductmouldquota.factoryId = v;
                productProductmouldquota.workshopId = null;
                stationIdList = [];
                try {
                  workshopIdList = await DynamicDataApi.getModelSelect(
                      active: false,
                      model: "hr.department",
                      name: "complete_name",
                      key: 'parent_id',
                      val: int.parse(v));
                } catch (e) {
                  workshopIdList = [];
                }
                setState(() {});
              }
            },
          ),
          CrySelect(
            label: '车间名称',
            //  width: 280,
            dataList: workshopIdList,
            value: productProductmouldquota.workshopId,
            onSaved: (v) => {productProductmouldquota.workshopId = v},
            onChange: (v) async {
              if (productProductmouldquota.workshopId != v) {
                formKey.currentState!.save();
                productProductmouldquota.workshopId = v;
                productProductmouldquota.stationId = null;
                try {
                  stationIdList = await DynamicDataApi.getModelSelect(
                      active: false,
                      model: "base.productionstation",
                      name: "stationname",
                      key: 'workshop_id',
                      val: int.parse(v));
                } catch (e) {
                  stationIdList = [];
                }
                setState(() {});
              }
            },
          ),
          CrySelect(
            label: '设备工位',
            //width: 280,
            dataList: stationIdList,
            value: productProductmouldquota.stationId,
            onSaved: (v) => {productProductmouldquota.stationId = v},
          ),
          CryInput(
            label: '切换前产品名称',
            value: productProductmouldquota.beforeproductName,
            onSaved: (v) {
              productProductmouldquota.beforeproductName = v;
            },
            onTap: () => selectProduct(true),
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            label: '切换后产品名称',
            value: productProductmouldquota.afterproductName,
            onSaved: (v) {
              productProductmouldquota.afterproductName = v;
            },
            onTap: () => selectProduct(false),
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInputNum(
            label: '单价',
            value: productProductmouldquota.mouldquota,
            onSaved: (v) {
              productProductmouldquota.mouldquota = v;
            },
          ),
          CryInput(
            label: '备注',
            value: productProductmouldquota.remark,
            onSaved: (v) {
              productProductmouldquota.remark = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            productProductmouldquota.active = '1';
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            ProductProductmouldquotaOdooApi.saveOrUpdate(
                    productProductmouldquota.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
